<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/common-head.jsp"%>

<body>

<!-- begin #page-container -->


    <!-- begin #content -->
    <div id="content" class="content">

        <%@include file="../common/common-breadcrumb-add.jsp" %>

        <!-- begin row -->
        <div class="row">
            <!-- begin col-6 -->
            <div class="col-xl-6">
                <!-- begin panel -->
                <div class="panel panel-inverse" data-sortable-id="form-stuff-1">
                    <!-- begin panel-heading -->
                    <div class="panel-heading">
                        <h4 class="panel-title" >${editObj!=null?'编辑用户':'新增用户'}</h4>
                        <div class="panel-heading-btn">
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-default" data-click="panel-expand"><i class="fa fa-expand"></i></a>
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-success" data-click="panel-reload"><i class="fa fa-redo"></i></a>
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-warning" data-click="panel-collapse"><i class="fa fa-minus"></i></a>
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-danger" data-click="panel-remove"><i class="fa fa-times"></i></a>
                        </div>
                    </div>
                    <!-- end panel-heading -->
                    <!-- begin panel-body -->
                    <div class="panel-body">
                        <form>
                            <input type="hidden" value="${editObj!=null?editObj.id:''}" id="id">
                            <input type="hidden" value="${editObj!=null?editObj.resId:''}" id="resId">
                            <div class="form-group row m-b-15">
                                <label class="col-form-label col-md-3">用户名</label>
                                <div class="col-md-9">
                                    <input type="email" class="form-control m-b-5" placeholder="用户名" name="userName" id="userName" value="${editObj!=null?editObj.userName:''}" />
                                    <small class="f-s-12 text-grey-darker" style="display: none">用户名.</small>
                                </div>
                            </div>

                            <div class="form-group row m-b-15">
                                <label class="col-form-label col-md-3">密码</label>
                                <div class="col-md-9">
                                    <input type="email" class="form-control m-b-5" placeholder="密码" name="password" id="password" value="${editObj!=null?editObj.password:''}" />
                                    <small class="f-s-12 text-grey-darker" style="display: none">密码.</small>
                                </div>
                            </div>

                            <c:forEach items="${menus}" var="menu" varStatus="menuStat">
                            <div class="form-group row m-b-15" >
                                <label class="col-md-3 col-form-label pt-1" >${menuStat.index==1?'菜单权限':''}</label>
                                <div class="col-md-2">
                                    <div class="custom-control custom-checkbox mb-1">
                                        <input type="checkbox" class="custom-control-input" id="menu_${menu.id}" value="${menu.id}" ${commUtils.contains(menuIds,menu.id)?'checked':''}>
                                        <label class="custom-control-label" for="menu_${menu.id}" >${menu.name}</label>
                                    </div>
                                </div>
                                <div class="col-md-7">
                                    <c:forEach items="${menu.childs}" varStatus="childStat" var="child">
                                    <div class="custom-control custom-checkbox mb-1" onshow="${menu.childs!=null && menu.childs.size()>0}">
                                        <input type="checkbox" class="custom-control-input" id="child_${menu.id}_${child.id}" value="${child.id}" ${commUtils.contains(menuIds,child.id)?'checked':''}>
                                        <label class="custom-control-label" for="child_${menu.id}_${child.id}" >${child.name}</label>
                                    </div>
                                    </c:forEach>
                                </div>
                            </div>
                            </c:forEach>

                            <div class="form-group row">
                                <div class="col-md-7 offset-md-3">
                                    <button type="button" class="btn btn-sm btn-primary m-r-5" id="saveObj">保存</button>
                                    <button type="button" class="btn btn-sm btn-default" onclick="back()">返回</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <!-- end panel-body -->

                </div>
                <!-- end panel -->

            </div>
            <!-- end col-6 -->
        </div>
        <!-- end row -->

    </div>
    <!-- end #content -->

<%@include file="../common/common-script.jsp"%>


<script>
    var ctx = '${basePath}'

    function back(){
        window.location.href=ctx+"/user/list"
    }

    $(function(){

        $("input[id^='menu_']").click(function(){
            var pid = $(this).val()
            $("input[id^='child_"+pid+"_']:checkbox").prop('checked',this.checked)
        })

        $("#saveObj").click(function(){
            var id = $("#id").val()
            var userName = $("#userName").val()
            var password = $("#password").val()
            var resId = $("#resId").val()

            var ids = []
            $("input[id^='menu_']:checked").each(function(){
                ids.push($(this).val())
            })
            $("input[id^='child_']:checked").each(function(){
                ids.push($(this).val())
            })


            $.ajax({
                type:'post',
                url:ctx+'user/add',
                data:JSON.stringify({"id":id,"userName":userName,"password":password,"resId":resId,"ids":ids}),
                success:function(res){
                    if(res == "1000"){
                        window.location.href=ctx+"/user/list"
                    }
                }
            })

        })
    })

</script>

</body>
</html>
